﻿#pragma once

#include "Common.h"
#include "ObjectPool.h"
#include "PageMap.h"

//单例模式—饿汉模式
class PageCache
{
public:
	static PageCache* getInstance()
	{
		return &_sInstance;
	}

	// 获取⼀个K⻚的span
	Span* newSpan(size_t k);

	// 获取从map对象pageid到span*的映射
	Span* MapObjectToSpan(void* obj);

	//将释放的span还给PageCache
	void ReleaseSpanToPageCache(Span* span);

	std::mutex& getMutex()
	{
		return _pageMtx;
	}

private:
	SpanList _spanLists[PAGE_SIZE]; //哈希桶
	std::mutex _pageMtx; // 定义一把锁，保护临界区资源
	//std::unordered_map<PAGE_ID, Span*> _idSpanMap;//保存pageid和span的映射关系，方便回收内存
	TCMalloc_PageMap1<32 - PAGE_SHIFT> _idSpanMap;
	//私有化构造函数
	PageCache() {}

	//防拷贝
	PageCache(const PageCache&) = delete;
	PageCache& operator=(const PageCache&) = delete;

	static PageCache _sInstance;
	ObjectPool<Span> _objPool;
};